package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.tradewar.TradeWarNotice;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

public class TradeWarNoticeDaoImpl extends TempDao {

	public LinkedList<TradeWarNotice> getTradeWarNoticeList(long selectTime) {
		String sql = "select * from t_u_trade_war_notice where noticeTime >=? order by noticeTime desc";
		ResultSet rs = null;
		CallableStatement call = null;
		LinkedList<TradeWarNotice> dataList = new LinkedList<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, selectTime);
			rs = call.executeQuery();
			while (rs.next()) {
				TradeWarNotice data = initNotice(rs);
				dataList.addFirst(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	public boolean addTradeWarNoticeList(List<TradeWarNotice> noticeList) {
		String sql = "INSERT INTO t_u_trade_war_notice(userId,userBaseInfo,killPatronsCount,noticeTime,enemyUserId,enemyBaseInfo,battleType," +
				"isAllKill) VALUES(?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
			for (TradeWarNotice notice : noticeList) {
				Map<Integer, DbParameter> para = new HashMap<>();
				int i = 1;
				para.put(i++, new DbParameter(Types.BIGINT, notice.getUserId()));
				para.put(i++, new DbParameter(Types.NVARCHAR, notice.getUserBaseInfoJsonStr()));
				para.put(i++, new DbParameter(Types.INTEGER, notice.getKillPatronsCount()));
				para.put(i++, new DbParameter(Types.BIGINT, notice.getNoticeTime()));
				para.put(i++, new DbParameter(Types.BIGINT, notice.getEnemyUserId()));
				para.put(i++, new DbParameter(Types.NVARCHAR, notice.getEnemyBaseInfoJsonStr()));
				para.put(i++, new DbParameter(Types.INTEGER, notice.getBattleType()));
				para.put(i++, new DbParameter(Types.BOOLEAN, notice.isAllKill()));

				mapList.add(para);
			}
			return getDb().executeBatchQuery(sql, mapList) > -1;
		}catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	private TradeWarNotice initNotice(ResultSet rs) throws SQLException {
		TradeWarNotice data = new TradeWarNotice();
		data.setUserId(rs.getLong("userId"));
		String baseUserInfoStr = rs.getString("userBaseInfo");
		if(null == baseUserInfoStr){
			baseUserInfoStr = "";
		}
		UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
		if(null != userBaseInfo) {
			data.setUserBaseInfo(userBaseInfo);
		}
		data.setKillPatronsCount(rs.getInt("killPatronsCount"));
		data.setNoticeTime(rs.getLong("noticeTime"));
		data.setEnemyUserId(rs.getLong("enemyUserId"));
		baseUserInfoStr = rs.getString("enemyBaseInfo");
		if(null == baseUserInfoStr){
			baseUserInfoStr = "";
		}
		userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
		if(null != userBaseInfo) {
			data.setEnemyBaseInfo(userBaseInfo);
		}
		data.setBattleType(rs.getInt("battleType"));
		data.setAllKill(rs.getBoolean("isAllKill"));

		return data;
	}
}
